package com.deep.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deep.common.utils.PageUtils;
import com.deep.common.utils.Query;
import com.deep.product.dao.AttrAttrgroupRelationDao;
import com.deep.product.dao.AttrDao;
import com.deep.product.dao.AttrGroupDao;
import com.deep.product.model.entity.AttrAttrgroupRelationEntity;
import com.deep.product.model.entity.AttrEntity;
import com.deep.product.model.entity.AttrGroupEntity;
import com.deep.product.model.vo.AttrGroupWithAttrsVO;
import com.deep.product.model.vo.SpuItemAttrGroupVO;
import com.deep.product.service.AttrGroupService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private AttrDao attrDao;
    @Autowired
    private AttrGroupDao attrGroupDao;
    @Autowired
    private AttrAttrgroupRelationDao relationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catalogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();

        if (StringUtils.hasLength(key)) {
            wrapper.and((obj) ->
                    obj.eq("attr_group_id", key).or().like("attr_group_name", key)
            );
        }

        if (catalogId == 0) {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        } else {
            wrapper.eq("catelog_id", catalogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrGroupWithAttrsVO> getAttrGroupWithAttrByCatalogId(Long catalogId) {
        // 1.查询当前分类下的所有属性分组
        List<AttrGroupWithAttrsVO> attrGroupWithAttrsVOS = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", 225L)).stream()
                .map(item -> {
                    AttrGroupWithAttrsVO groupWithAttrsVO = new AttrGroupWithAttrsVO();
                    BeanUtils.copyProperties(item, groupWithAttrsVO);
                    return groupWithAttrsVO;
                }).collect(Collectors.toList());

        // 2.查询每个分组的所有信息
        for (AttrGroupWithAttrsVO attrGroupWithAttrsVO : attrGroupWithAttrsVOS) {
            Long attrGroupId = attrGroupWithAttrsVO.getAttrGroupId();
            List<AttrAttrgroupRelationEntity> relationEntities = relationDao.selectList(
                    new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId));

            List<Long> attrIds = relationEntities
                    .stream().map(item -> item.getAttrId()).collect(Collectors.toList());
            if (attrIds.size() > 0) {
                List<AttrEntity> attrEntities = attrDao.queryByIds(attrIds);
                attrGroupWithAttrsVO.setAttrs(attrEntities);
            }
        }
        return attrGroupWithAttrsVOS;
    }

    @Override
    public List<SpuItemAttrGroupVO> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        //1、查出当前spu对应的所有属性的分组信息以及当前分组下的所有属性对应的值
        List<SpuItemAttrGroupVO> vos = baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
        return vos;
    }


}